package com.yunlong.controller;

import com.yunlong.common.UserInfoDto;
import com.yunlong.common.controller.BaseController;
import com.yunlong.common.exception.SysException;
import com.yunlong.dto.LoginSuccessDto;
import com.yunlong.dto.MessageDto;
import com.yunlong.dto.MessageSuccessDto;
import com.yunlong.dto.getUserDto;
import com.yunlong.form.*;
import com.yunlong.service.ManagerService;
import com.yunlong.service.UserService;
import com.yunlong.wrapper.ResultWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author guorui
 * @date 2020-07-31 15:11
 */
@CrossOrigin
@Slf4j
@Api(value = "ManagerController", tags = {"管理员API(后台管理系统)"})
@RestController
@RequestMapping("/manager")
@Transactional
public class ManagerController extends BaseController<UserInfoDto> {
    @Autowired
    private ManagerService managerService;
    /**
     * 普通用户登录
     */
    @ApiOperation("管理员登录")
    @PostMapping("/managerLogin")
    public ResultWrapper managerLogin(@Valid @RequestBody ManagerLoginForm managerLoginForm) {
        try {
            LoginSuccessDto loginSuccessDto = managerService.managerLogin(managerLoginForm);
            return ResultWrapper.successWithData(loginSuccessDto);
        } catch (SysException e) {
            log.info("UserController.register");
            return ResultWrapper.failure(e.getMessage());
        }
    }
    /**
     * 管理员退出登录
     * @TODO 目前退出登录有问题，因为拦截器中判断的是普通员工，但管理员不行
     */
    @ApiOperation("管理员退出登录")
    @PostMapping("/managerQuit")
    public ResultWrapper managerQuit(){
        log.info("userID:"+getCurrentUserInfo().getUserId());
        String situation = managerService.logOut(getCurrentUserInfo().getUserId());
        return ResultWrapper.successWithData(situation);
    }

    /**
     * 管理员单个新增用户，一次只能新增一个
     * @Valid 校验form表单的字段格式
     */
    @ApiOperation("管理员单个新增用户")
    @PostMapping("/userRegister")
    public ResultWrapper userRegister(@Valid @RequestBody UserRegisterForm userRegisterForm) {
        try {
            return ResultWrapper.successWithData(managerService.userRegister(userRegisterForm));
        } catch (SysException e) {
            log.info("UserController.register");
            return ResultWrapper.failure(e.getMessage());
        }
    }
    /**
     * 管理员批量新增用户
     */
    @ApiOperation("管理员批量新增用户")
    @PostMapping("/usersRegister")
    public ResultWrapper usersRegister(@RequestParam("file") MultipartFile file) {
        String name=file.getOriginalFilename();
        if(name.length()<6|| !name.substring(name.length()-5).equals(".xlsx")){
            return ResultWrapper.failure("文件格式错误");
        }
        if (Objects.isNull(file) || file.isEmpty()) {
            return ResultWrapper.failure("文件为空文件");
        }
        MessageSuccessDto registerSuccessDto = null;
        try {
            registerSuccessDto = managerService.uploadUsersFile(file.getInputStream());
            return ResultWrapper.successWithData(registerSuccessDto);
        } catch (IOException e) {
            return ResultWrapper.failure(e.getMessage());
        }

    }
    /**
     * 管理员删除用户
     */
    @ApiOperation("管理员删除用户")
    @PostMapping("/deletedUsers")
    public ResultWrapper deletedUsers(@Valid @RequestBody UserApprovalForm userApprovalForm){
        try {
            return ResultWrapper.successWithData(managerService.deletedUsers(userApprovalForm));
        } catch (SysException e) {
            return ResultWrapper.failure(e.getMessage());
        }
    }
    /**
     * 管理员删除用户
     */
    @ApiOperation("超级管理员删除管理员")
    @PostMapping("/deletedManagers")
    public ResultWrapper deletedManagers(@Valid @RequestBody ManagerIdForm managerIdForm){
        try {
            return ResultWrapper.successWithData(managerService.managerDeleted(managerIdForm));
        } catch (SysException e) {
            return ResultWrapper.failure(e.getMessage());
        }
    }
    /**
     * 超级管理员新增管理员（后台管理系统）
     * @Valid 校验form表单的字段格式
     */
    @ApiOperation("超级管理员新增管理员")
    @PostMapping("/managerRegister")
    public ResultWrapper managerRegister(@Valid @RequestBody ManagerRegisterForm managerRegisterForm) {
        try {
            return ResultWrapper.successWithData(managerService.managerRegister(managerRegisterForm));
        } catch (SysException e) {
            log.info("UserController.register");
            return ResultWrapper.failure(e.getMessage());
        }
    }

    /**
     * 部门管理界面，返回所有部门的学员
     */
    @ApiOperation(value = "部门管理界面返回所有部门的学员", produces = "application/json")
    @GetMapping("/allUsers")
    public ResultWrapper allRegisters() {
        return ResultWrapper.successWithData(managerService.allUsers());
    }
    /**
     * 给超级管理员展示所有管理员
     */
    @ApiOperation(value = "显示所有管理员", produces = "application/json")
    @GetMapping("/allManagers")
    public ResultWrapper allManagers() {
        return ResultWrapper.successWithData(managerService.allManagers());
    }
    /**
     * 管理员注销用户账号（user_status置为-1）
     */
    @ApiOperation("管理员冻结用户账号")
    @PutMapping("/rejectRegister")
    public ResultWrapper logOff(@Valid @RequestBody UserApprovalForm userRejectForm) {
        try {
            return ResultWrapper.successWithData(managerService.rejectRegister(userRejectForm));
        } catch (SysException e) {
            log.info("注册申请失败");
            System.out.println("异常");
            return ResultWrapper.failure(e.getMessage());
        }
    }

    /**
     * 管理员恢复用户账号
     */
    @ApiOperation("管理员恢复用户账号")
    @PutMapping("/approvalUsers")
    public ResultWrapper ApprovalRegister(@Valid @RequestBody UserApprovalForm userApprovalForm) {
        try {
            return ResultWrapper.successWithData(managerService.approvalRegister(userApprovalForm));
        } catch (SysException e) {
            log.info("注册申请异常");
            System.out.println("异常");
            return ResultWrapper.failure(e.getMessage());
        }
    }
    /**
     * 管理员查询用户（管理员）
     */
    @ApiOperation(value = "管理员按姓名或学号查询用户", produces = "application/json")
    @GetMapping("/selectUser")
    public ResultWrapper selectUser(String userInfo){
        log.info(userInfo);
        List<getUserDto> getUserDto = managerService.selectUser(userInfo);
        return ResultWrapper.successWithData(getUserDto);
    }
}
